package thefirst.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sun.xml.internal.ws.client.SenderException;

import thefirst.code.model.CodeService;
import thefirst.code.model.CodeVO;

@Controller
public class CodeController {
	@Resource(name="codeServiceImpl")
	private CodeService codeService;
	@RequestMapping("/findGenderByCode.do")
	@ResponseBody
	public List<CodeVO> findGenderByCode() throws SQLException{
		List<CodeVO> cList=codeService.findGenderByCode();
		System.out.println(cList);
		return cList;
	}
	@RequestMapping("/getTotalCodeList.do")
	public ModelAndView getTotalCodeList(String pageNo) throws SQLException{
		return new ModelAndView("admin/code/getTotalCodeList.admin","list",codeService.getTotalCodeList(pageNo));
	}
	@RequestMapping("/insertCodePage.do")
	public String insertCodePage() throws SQLException{
		return "admin/code/insertCodePage.admin";
	}
	@RequestMapping("/isExistCode.do")
	@ResponseBody
	public int isExistCode(String code_code) throws SQLException{
		System.out.println("code :"+code_code);
		int result=codeService.isExistCode(code_code);
		System.out.println("code duplicate test:"+result);
		return result;
	}
	@RequestMapping("/insertCode.do")
	@ResponseBody
	public int insertCode(HttpServletResponse response, CodeVO cvo) throws SQLException, IOException{
		int result=codeService.insertCode(cvo);
		if(result==0){
			response.sendRedirect("getTotalCodeList.do");
		}
		return result;
	}
	@RequestMapping("/updateCodePage.do")
	public ModelAndView updateCodePage(int code_no) throws SQLException{
		CodeVO cvo=codeService.selectCodeInfo(code_no);
		System.out.println(cvo);
		return new ModelAndView("admin/code/updateCodePage.admin","cvo",cvo);
	}
	@RequestMapping("/updateCode.do")
	@ResponseBody
	public int updateCode(HttpServletResponse response,CodeVO cvo) throws SQLException, IOException{
		int result=codeService.updateCode(cvo);
		if(result==0){
			response.sendRedirect("getTotalCodeList.do");
		}
		return result;
	}
	@RequestMapping("/deleteCode.do")
	public ModelAndView deleteCode(String code_no) throws SQLException{
		codeService.deleteCode(code_no);
		return new ModelAndView("redirect:/getTotalCodeList.do");
	}
}
